<?PHP

	include("../includes/configPages.php");
	include("../includes/forum/category.class.php");
	include("../includes/forum/board.class.php");
	include("../includes/forum/topic.class.php");
	$category = new category();
	$board = new board();
	$topic = new topic();

	$outHtml = "";
	
	if(is_numeric($_GET['args'])){
		$boardId = $db->prep($_GET['args']);
		$currentBoard = $board->getBoard($db, $boardId);
			
		if(mysql_num_rows($currentBoard)==0){
			$variableStack['board_name'] = $lang['board_not_exists'];
			$variableStack['board_desc'] = $lang['board_not_exists_desc'];
		} else {
			while($row = mysql_fetch_object($currentBoard)){
				$variableStack['board_name'] = $row->board_name;
				$variableStack['board_desc'] = $row->board_desc;
			}
		}
		/**/
				
		$breadRes = $board->getBread($db, $boardId);
	
		/* pagination jquery stuff */
		if($breadRes!=false){
			$variableStack['board_link'] = "#board:".$breadRes['boa_id']; 
			$variableStack['board_text'] = $breadRes['boa_name']; 
			$variableStack['category_link'] = "#forum"; 
			$variableStack['category_text'] = $breadRes['cat_name']; 
			$variableStack['board_id'] = $breadRes['boa_id']; 
			$variableStack['category_id'] = $breadRes['cat_id']; 
			
			if($users->auth($db, 2)){
				$variableStack['h_del']="";
				if($users->getVal($db, 'hide_deleted', $_SESSION['uid'])==1){
					$variableStack['checked']="checked='checked'";
					$variableStack['hide_status']="hidden";
				} else {
					$variableStack['checked']="";
					$variableStack['hide_status']="unhidden";
				}
			} else {
				$variableStack['h_del']="_hide";
			}
		
			$boardHtml = file_get_contents('../styles/'.$activeStyle.'/t/board.html');
			
			// check if we should display deleted posts
			if(is_numeric($_SESSION['uid'])){
				$result = $db->query("SELECT hide_deleted FROM ".$prefix."users WHERE userid=".$db->prep($_SESSION['uid']));
				$hide_deleted = mysql_fetch_object($result);
				if(mysql_num_rows($result)==1){
					if($hide_deleted->hide_deleted==1){
						$show = 1;	
					} else {
						$show = NULL;	
					}
					$topics = $topic->getAllTopics($db, $boardId, NULL, $show);
				} else {
					$topics = $topic->getAllTopics($db, $boardId);
				}
			} else {
				$topics = $topic->getAllTopics($db, $boardId);
			}
				if(mysql_num_rows($topics)==0){
					$html = file_get_contents('../styles/'.$activeStyle.'/t/forum_board_topic.html');
					$topicVariableStack['topic_name'] = $lang['topic_not_exists'];
					$topicVariableStack['topic_desc'] = $lang['topic_not_exists_desc'];
					$topicVariableStack['topic_replies'] = "0";
					$topicVariableStack['topic_views'] = "0";
					$topicVariableStack['topic_last_post'] = $lang['topic_never'];
					$topicVariableStack['topic_link'] = "";
					$topicVariableStack['modifier'] = "_empty";
					$outHtml .= '<div class="result">';
						$outHtml .= parseHtml($html, $topicVariableStack);
					$outHtml .= '</div>';
				} else {
					$count = 0;
					$outHtml .= '<div class="result">';
						while($row = mysql_fetch_object($topics)){
							if(($row->deleted==1 && $users->auth($db, 2)) || $row->deleted==0){
								$html = file_get_contents('../styles/'.$activeStyle.'/t/forum_board_topic.html');
								if($row->locked==1){
									$topicVariableStack['locked_tag'] = "_locked";
									$topicVariableStack['icon_tag'] = "_locked";
									$stickyText = $lang['topic_locked'];
								} else {
									$stickyText = "";	
								}
								if($row->topic_stickied==1){
									$topicVariableStack['icon_tag'] = "_sticky";
									$stickyText .= $lang['topic_sticky'];
								} else {
									$stickyText .= "";
								}
								$topicVariableStack['topic_name'] = $stickyText. stripslashes($row->topic_name);
								$topicVariableStack['topic_desc'] = stripslashes($row->topic_desc);
								$topicVariableStack['topic_replies'] = $row->topic_replies;
								$topicVariableStack['topic_views'] = $row->topic_views;
								$topicVariableStack['topic_link'] = "#topic:".$row->topic_id;
								$topicVariableStack['topic_id'] = $row->topic_id;
								$lastPostTime = $row->topic_time;
								$nowTime = time();
								$difference = $nowTime - $lastPostTime;
								if($difference<86400){ //1 day
									if(	$difference/3600 < 1 ){
										if($difference < 60){
											$timeAgo = $lang['time_less_minute'];
										} else {
											$timeAgo = sprintf($lang['time_less_hour'],round($difference/60));
										}
									} else {
										$timeAgo = sprintf($lang['time_over_hour'], round($difference/3600));
									}
								} else {
									$timeAgo = sprintf($lang['time_over_day'], round($difference/86400));
								}
								$topicVariableStack['topic_last_post'] = $timeAgo;
								if($count % 10 == 0 && $count!=0){
									$outHtml .= '</div><div class="result">';
								}
								
								/* mod controls */
								if($users->auth($db, 2)){
									if($row->locked==1){
										$topicVariableStack['lockunlock_control'] = "<a class='unlock_thread' alt='".$row->topic_id."'>unlock</a>";
									} else {
										$topicVariableStack['lockunlock_control'] = "<a class='lock_thread' alt='".$row->topic_id."'>lock</a>";
									}
									if($row->deleted==1){
										$topicVariableStack['delete_control'] = "<a class='undelete_thread' alt='".$row->topic_id."'>undelete</a>";
										$topicVariableStack['delete_flag'] = "_deleted";
									} else {
										$topicVariableStack['delete_control'] = "<a class='delete_thread' alt='".$row->topic_id."'>delete</a>";
									}
								} else {
									$topicVariableStack['mod_controls'] = "";
								}
								$outHtml .= parseHtml($html, $topicVariableStack);
								$topicVariableStack['delete_flag'] = "";
								$topicVariableStack['locked_tag'] = "";
								$topicVariableStack['icon_tag'] = "";
								$stickyText="";
								$count++;
							}
						}
					$outHtml .= '</div>';
				}
				
				$variableStack['board_html'] = $outHtml;
				echo parseHtml($boardHtml, $variableStack);

	} else {
			$html = file_get_contents('../styles/'.$activeStyle.'/t/board-none.html');
			$variableStack['board_name'] = "Board does not exist";
			$variableStack['board_desc'] = "Sorry!";
			echo parseHtml($html, $variableStack);
		}
	} else {
			$html = file_get_contents('../styles/'.$activeStyle.'/t/board-none.html');
			$variableStack['board_name'] = "Board does not exist";
			$variableStack['board_desc'] = "Sorry!";
			echo parseHtml($html, $variableStack);
	}
?>